//
//  TextViewViewController.swift
//  DemoProject
//
//  Created by hao on 2018/7/18.
//  Copyright © 2018年 hao. All rights reserved.
//

import UIKit

class TextViewViewController: BaseTableViewController {

    lazy var textView : UITextView = {
        let _textV = UITextView()
        self.tableHeadView.addSubview(_textV)
        _textV.snp.makeConstraints({ (make) in
            make.left.top.equalTo(15)
            make.right.bottom.equalTo(-15)
        })
        return _textV
    }()
    
    let disposeBag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        textView.text = "多行文本输入框（UITextView）的用法: http://www.hangge.com/blog/cache/detail_531.html     测试的电话号码13148478648"
        _rxSwiftPropert()
        // 连接效果必须同时设置 textView.isEditable = false 才有效果
//        textView.dataDetectorTypes = UIDataDetectorTypes.all
//        textView.isEditable = false
        let mail = UIMenuItem(title: "邮件", action: #selector(onEmail))
        let testMenu = UIMenuItem(title: "TEST", action: #selector(onTest))
        UIMenuController.shared.menuItems = [mail, testMenu]
        textView.inputAccessoryView = UIButton(type: .infoDark)
        textView.clearsOnInsertion = true
        setupData()
    }
    
    @objc func onEmail() {
        print(info: "点击邮件")
    }
    
    @objc func onTest() {
        print(info: "点击TEST")
    }
    
    /// UITextView 还封装了如下几个委托回调方法：(其他可参考textField)
    /// didBeginEditing：开始编辑
    /// didEndEditing：结束编辑
    /// didChange：编辑内容发生改变
    /// didChangeSelection：选中部分发生变化
    private func _rxSwiftPropert() {
        // 开始编辑响应
        textView.rx.didBeginEditing.subscribe(onNext: { print(info: "开始编辑\($0)") }).disposed(by: disposeBag)
        // 结束编辑响应
        textView.rx.didEndEditing.subscribe(onNext: { print(info: "结束编辑") }).disposed(by: disposeBag)
        // 内容发生变化响应
        textView.rx.didChange.subscribe(onNext: { print(info: "内容发生改变") }).disposed(by: disposeBag)
        // 选中部分变化响应
        textView.rx.didChangeSelection.subscribe(onNext: { print(info: "选中部分发生变化") }).disposed(by: disposeBag)
    }
    
    private func setupData() {
        let titles = ["是否可编辑",
                      "内容是否可选",
                      "文字中的电话号码和网址自动加链接",
                      "自定义选择内容后的菜单",
                      "inputView(弹出的视图，默认键盘)",
                      "inputAccessoryView",
                      "clearsOnInsertion"]
        
        let descArr = ["textview.isEditable = false",
                       "textview.isSelectable = false",
                       """
                        textView.isEditable = false // 必须设置不然没有链接效果
                        textview.dataDetectorTypes = [] //都不加链接
                        textview.dataDetectorTypes = UIDataDetectorTypes.phoneNumber //只有电话加链接
                        textview.dataDetectorTypes = UIDataDetectorTypes.link //只有网址加链接
                        textview.dataDetectorTypes = UIDataDetectorTypes.all //电话和网址都加
                       """,
                       """
                        let mail = UIMenuItem(title: "邮件", action: #selector(onEmail))
                        let testMenu = UIMenuItem(title: "TEST", action: #selector(onTest))
                        UIMenuController.shared.menuItems = [mail, testMenu]
                       """,
                       """
                        设置从底部弹出的视图
                        textView.inputView = [[UIDatePicker alloc]init];
                       """,
                       """
                        设置弹出视图上方的辅助视图
                        textView.inputAccessoryView
                       """,
                       "设置textView获得焦点，会选中所有已经存在的输入内容"]
        
        addTitles(titles, footDescs: descArr)
    }

    override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        textView.resignFirstResponder()
    }
    
}
